package cn.four.hqServer.modules.userregister.controller;

import cn.four.hqServer.common.entity.HqUserInfo;
import cn.four.hqServer.common.result.Result;
import cn.four.hqServer.modules.userregister.dto.UserDto;
import cn.four.hqServer.modules.userregister.service.RegisterService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.regex.Pattern;

@RestController
public class RegisterController {
    @Autowired
    private RegisterService registerService;

    @PostMapping("/register")
    public Result register(@RequestBody UserDto userDto){
        //数据校验
        if("".equals(userDto.getUsername()) || "".equals(userDto.getUserId()) || "".equals(userDto.getAddress()) ||
                "".equals(userDto.getPassword()) || "".equals(userDto.getEmail()) ||
                "".equals(userDto.getGender()) || "".equals(userDto.getNumber()) ||
                "".equals(userDto.getBirthday())){
            return Result.error("请输入全部信息");
        }
        if( !Pattern.matches("^1[3-9]\\d{9}$", userDto.getNumber())){
            System.out.println(Pattern.matches("^1[3-9]\\d{9}$", userDto.getNumber()));
            System.out.println(userDto.getNumber());
            return Result.error("输入的手机号码格式有错误，请检查后重试");
        }
        if( !Pattern.matches("[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+",userDto.getEmail())){
            return Result.error("输入的邮箱格式有错误，请检查后重试！");
        }
        if( !Pattern.matches("^[1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}([0-9]|(X|x))",
                userDto.getUserId())){
            return Result.error("输入的身份证号格式有错误，请检查后重试！");
        }
        if( !Pattern.matches("\\d{6}", userDto.getPassword())){
            return Result.error("输入的支付密码格式有错误，请检查后重试");
        }
        if(!registerService.selectExistUserByUserId(userDto.getUserId())){
            return Result.error("该身份证号已经被注册");
        } else if (!registerService.selectExistUserByNumber(userDto.getNumber())) {
            return Result.error("该电话号码号码已经被注册");
        }
        HqUserInfo h = new HqUserInfo();
        h.setUsername(userDto.getUsername());
        h.setUserId(userDto.getUserId());
        h.setAddress(userDto.getAddress());
//        String userDtoBirthday = userDto.getBirthday();
//        String[] split = userDtoBirthday.split("\\.");
//        LocalDateTime birthday = LocalDateTime.of(Integer.parseInt(split[0]), Month.of(Integer.parseInt(split[1])), Integer.parseInt(split[2]), 0, 0);
        h.setBirthday(userDto.getBirthday());
        h.setPassword(userDto.getPassword());
        h.setEmail(userDto.getEmail());
        h.setGender(userDto.getGender());
        h.setNumber(userDto.getNumber());
        //新增用户成功，返回200，否则返回300
        if (registerService.insertUser(h)) {
            return Result.success("注册成功");
        }
        return Result.error("该用户已存在");
    }
}
